forum

home / developersection / forums / check if a property exist in a class

Check if a property exist in a class

Anonymous User 2559 12-Mar-2013

Hi Expert!

I try to know if a property exist in a class, I tried this :

public static bool HasProperty(this object obj, string propertyName)
{
    return obj.GetType().GetProperty(propertyName) != null;
}
I don't understand why the first test method does not pass ?

[TestMethod]
public void Test_HasProperty_True()
{
    var res = typeof(MyClass).HasProperty("Label");
    Assert.IsTrue(res);
}

[TestMethod]
public void Test_HasProperty_False()
{
    var res = typeof(MyClass).HasProperty("Lab");
    Assert.IsFalse(res);
}

Thanks in advance!


c# c# 
Updated on 12-Mar-2013

I am a content writter !


Message
Can you answer this question?

Answer

1 Answers

Liked By